package classes
{
	import flash.geom.Rectangle;
	
	import mx.core.UIComponent;
	
	public class Utils
	{
		public function Utils()
		{
		}

		public static function plotPoints(ar:Array, options:Object=null):UIComponent{
			
			var mm:Object = Utils.minMax(ar); 
			var mc:UIComponent = new UIComponent();
			mc.width = ar.length;
			mc.height = 100;
			mc.graphics.lineStyle(0,0);
			
			mc.graphics.moveTo(0,(ar[0]-mm.min)/mm.diff*100);
//			trace(0,ar[0]*100);
			for(var i:int = 1; i<ar.length; i++){
//				trace(i,ar[i]*100);
				mc.graphics.lineTo(i,(ar[i]-mm.min)/mm.diff*100);
			}
			if(mm.min<0 && 0<mm.max){
				mc.graphics.lineStyle(0,0xff0000);
				mc.graphics.moveTo(0,-mm.min/mm.diff*100);
				mc.graphics.lineTo(mc.width,-mm.min/mm.diff*100);
			}
			return mc;
		}
		
		public static function plotColumns(ar:Array, rect:Rectangle):UIComponent{
			var mc:UIComponent = new UIComponent();
			mc.width = rect.width;
			mc.height = rect.height;
			mc.graphics.lineStyle(0,0xff0000);
			for(var i:int=0; i<ar.length;i++){
				mc.graphics.moveTo(ar[i],0);
				mc.graphics.lineTo(ar[i],rect.height);
			}
			return mc;
		}
		
		public static function plotStriasX(ar:Array, rect:Rectangle):UIComponent{
			var mc:UIComponent = new UIComponent();
			mc.width = rect.width;
			mc.height = rect.height;
			mc.graphics.lineStyle(1, 0xff00ff);
			
			for each(var y:int in ar){
				mc.graphics.moveTo(0,y);
				mc.graphics.lineTo(mc.width,y);
			}
			
			return mc;
		}
		public static function minMax(ar:Array):Object{
			//find min/max
			
			var ret:Object = {
				min:Number.MAX_VALUE,
				max:Number.MIN_VALUE
			};
			
			for(var i:int=0;i<ar.length;i++){
				ret.max = Math.max(ret.max, ar[i]);
				ret.min = Math.min(ret.min, ar[i]);
			}
			ret.diff = ret.max - ret.min;
			return ret;
		}
	}
}